package io.geekidea.springboot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.springboot.entity.Device;
import io.geekidea.springboot.mapper.DeviceMapper;
import io.geekidea.springboot.service.IDeviceService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Override
    public boolean save(Device device) {
        device.setCreatedAt(LocalDateTime.now());
        device.setUpdatedAt(LocalDateTime.now());
        return super.save(device);
    }

    @Override
    public boolean updateById(Device device) {
        device.setUpdatedAt(LocalDateTime.now());
        return super.updateById(device);
    }


    @Override
    public Device getById(Long deviceId) {
        return baseMapper.selectDeivcegWithBuildId(deviceId);
    }


    public Page<Device> listByPage(int page, int size, Long buildingId) {
        Page<Device> pageInfo = new Page<>(page,size,true);
        List<Device> devices = baseMapper.selectDevicesWithBuild(buildingId, (page-1)*size, size);
        pageInfo.setRecords(devices);
        return pageInfo;
    }
}
